﻿using System;
using System.Web.UI.WebControls;

namespace Hxsoft.AppV1
{
	public static class DropDownListExtensions
	{
		public static void EnsurePrompt(this DropDownList o)
		{
			EnsurePrompt(o, Res.DropDownListExtensions_SelectPromptText);
		}

		public static void EnsurePrompt(this DropDownList o, string promptText)
		{
			if (o.Items.Count > 0 && (string.IsNullOrEmpty(o.Items[0].Value) && o.Items[0].Text == promptText)) return;
			o.Items.Insert(0, new ListItem(promptText, ""));
		}

		public static bool TrySelectValue(this DropDownList o, string selectValue)
		{
			if (o.Items.FindByValue(selectValue) == null)
				return false;
			
			o.SelectedValue = selectValue;
			return true;
		}
	}
}